﻿using System;
using System.Collections.Generic;
using System.Data.Common;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.Entity;
using System.Data.SQLite.EF6;
using System.IO;
using SQLite.CodeFirst;

namespace ConsoleApp1
{
    public class Student
    {
        public int ID { get; set; }
        public string Name { get; set; }
        public int Age { get; set; }
        public string Gender { get; set; }
    }

	public class StudentContext : DbContext
    {
        //定义属性，便于外部访问数据表
        public DbSet<Student> Students { get { return Set<Student>(); } }

        //public StudentContext() : base("SQLiteConnect")
        //{
        //    ConfigurationFunc();
        //}

        public StudentContext(DbConnection existingConnection, bool contextOwnsConnection) :
            base(existingConnection, contextOwnsConnection)
        {
            ConfigurationFunc();
        }
        private void ConfigurationFunc()
        {
            Configuration.LazyLoadingEnabled = true;
            Configuration.ProxyCreationEnabled = true;
        }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            ModelConfiguration.Configure(modelBuilder);
            var init = new SqliteDropCreateDatabaseWhenModelChanges<StudentContext>(modelBuilder);
            //var init = new SqliteCreateDatabaseIfNotExists<MyBookDB>(modelBuilder);
            Database.SetInitializer(init);
        }
    }

    public class ModelConfiguration
    {
        public static void Configure(DbModelBuilder modelBuilder)
        {
            ConfigureBookEntity(modelBuilder);
        }
        private static void ConfigureBookEntity(DbModelBuilder modelBuilder)
        {
            modelBuilder.Entity<Student>();
        }
    }



    class Program
    {
        static void Main(string[] args)
        {
            List<Student> books = new List<Student>()
            {
                //new Student() {ID=1, Name = "小明", Age = 11, Gender = "男" },
                //new Student() {ID=2, Name = "老王",  Age = 12, Gender = "男" },
                //new Student() {ID=3, Name = "小郭", Age = 10, Gender = "女" },
                //new Student() {ID=4, Name = "小李",  Age = 11, Gender = "男" },
                //new Student() {ID=5, Name = "小玉",  Age = 9, Gender = "女" },        
                
                new Student() {Name = "小明", Age = 11, Gender = "男" },
                new Student() {Name = "老王", Age = 12, Gender = "男" },
                new Student() {Name = "小郭", Age = 10, Gender = "女" },
                new Student() {Name = "小李", Age = 11, Gender = "男" },
                new Student() {Name = "小玉", Age = 9, Gender = "女" },
            };

            //using (var db = new StudentContext())
            //{
            //    db.Students.AddRange(books);
            //    int count = db.SaveChanges();
            //    Console.WriteLine($"{DateTime.Now}, 插入{count}条记录");
            //}

            var path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "abc2.db");
            var connection = SQLiteProviderFactory.Instance.CreateConnection();
            connection.ConnectionString = $"Data Source={path}";

            using (var db = new StudentContext(connection, false))
            {
                db.Students.AddRange(books);
                int cnt = db.SaveChanges();
                Console.WriteLine($"{DateTime.Now}，插入{cnt}条记录");
                foreach (var dbStudent in db.Students)
                {
                    Console.WriteLine($"{dbStudent.ID},{dbStudent.Name},{dbStudent.Age},{dbStudent.Gender}");
                    Console.WriteLine("");
                }

                //Console.WriteLine("");
                //var one = db.Students.Where(p => p.ID > 10 && p.ID < 13).ToList();
                //db.Students.RemoveRange(one);
                //db.SaveChanges();

                //foreach (var dbStudent in db.Students)
                //{
                //    Console.WriteLine($"{dbStudent.ID},{dbStudent.Name},{dbStudent.Age},{dbStudent.Gender}");
                //    Console.WriteLine("");
                //}




            }

            Console.ReadKey();

        }
    }
}
